package ar.com.fiuba.application.impl;

import org.springframework.transaction.annotation.Transactional;

import ar.com.fiuba.application.api.DataBaseLoader;
import ar.com.fiuba.application.api.DataSource;
import ar.com.fiuba.application.api.GenericDao;
import ar.com.fiuba.domain.Word;
import ar.com.fiuba.domain.api.WordFactory;
import ar.com.fiuba.domain.exceptions.DomainException;

public class DataBaseLoaderImpl implements DataBaseLoader {

	private WordFactory wordFactory;

	private GenericDao<Word> wordDao;

	public DataBaseLoaderImpl(WordFactory wordFactory, GenericDao<Word> wordDao) {
		this.wordFactory = wordFactory;
		this.wordDao = wordDao;
	}

	@Transactional
	public void loadData(DataSource dataSource) {
		while (dataSource.hasDataLeft()) {
			String dataItem = dataSource.getNextWord();
			Word word = null;
			try {
				word = wordFactory.makeWord(dataItem);
			} catch (DomainException e) {
				// TODO new log properly.
				e.printStackTrace();
			}
			if (word != null)
				wordDao.save(word);
		}
	}

}
